package com.doit.day02;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.record.TimestampType;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class KafkaConsumerDemo1 {
    public static void main(String[] args) {
//        KafkaConsumer<String, String> consumer = KafkaConsumerUtils.getConsumer("java", "test");
        Map<String, Object> map = new HashMap<>();
        //必须配置的
        map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        map.put(ConsumerConfig.GROUP_ID_CONFIG,"g02");
        //选配的
        map.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        //设置从哪里开始读取数据  latest  最新的
        map.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
        //允许自动提交偏移量  __consumer_offsets
        map.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        //自动提交偏移量的间隔时间
//        map.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"10000");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(map);
        //告诉他，干啥呀？消费哪一个topic中的数据

        //是针对topic级别的订阅主题
//        consumer.subscribe(Arrays.asList("java"));
//        TopicPartition partition0 = new TopicPartition("mysqlData", 1);
//        TopicPartition partition1 = new TopicPartition("mysqlData", 2);
        TopicPartition partition3 = new TopicPartition("java", 0);

        consumer.assign(Arrays.asList(/*partition0,partition1,*/partition3));

        //具体从什么地方开始读数据  --partition --offset
//        consumer.seek(partition0,0);
//        consumer.seek(partition1,0);
        consumer.seek(partition3,674);

/*        TopicPartition partition1 = new TopicPartition("mysqlData", 1);
        consumer.seek(partition1,0);
        TopicPartition partition2 = new TopicPartition("mysqlData", 2);
        consumer.seek(partition2,0);*/



        //消费数据
        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            consumer.commitSync();
            for (ConsumerRecord<String, String> record : poll) {
                String topic = record.topic();
                int partition = record.partition();
                long offset = record.offset();
                String value = record.value();
                long timestamp = record.timestamp();
                TimestampType timestampType = record.timestampType();
                System.out.println(topic+","+partition+","+offset+","+value+","+timestamp+","+timestampType);
            }


        }



    }
}
